package gold.digger;

import gold.utils.InputUtil;

/**
 * Created by fanzhenyu02 on 2021/12/10.
 * common problem solver template.
 */
public class LC1423 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {
        public int maxScore(int[] cardPoints, int k) {
            int size = cardPoints.length - k, n = cardPoints.length, windowMin = Integer.MAX_VALUE, curWindowSum = 0, sum = 0;
            for (int i = 0; i < size; i++) {
                curWindowSum += cardPoints[i];
            }

            sum = curWindowSum;
            windowMin = Math.min(windowMin, curWindowSum);
            for (int i = size; i < n; i++) {
                sum += cardPoints[i];
                curWindowSum += cardPoints[i] - cardPoints[i - size];
                windowMin = Math.min(windowMin, curWindowSum);
            }

            return sum - Math.min(curWindowSum, windowMin);
        }
    }

    public void run() {
        System.out.println(new Solution().maxScore(InputUtil.toIntegerArray("[1,2,3,4,5,6,1]"), 3));
        System.out.println(new Solution().maxScore(InputUtil.toIntegerArray("[2,2,2]"), 2));
        System.out.println(new Solution().maxScore(InputUtil.toIntegerArray("[9,7,7,9,7,7,9]"), 7));
        System.out.println(new Solution().maxScore(InputUtil.toIntegerArray("[1,1000,1]"), 1));
        System.out.println(new Solution().maxScore(InputUtil.toIntegerArray("[1,79,80,1,1,1,200,1]"), 3));
    }

    public static void main(String[] args) throws Exception {
        LC1423 an = new LC1423();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
